# Only speak when requested.
ifndef VERBOSE
.SILENT:
endif

BASE_FILE=base.m4

cblue=$(shell echo -e '\033[34m')
cnorm=$(shell echo -e '\033[0m')

.PHONY: all allhosts

all:
	echo 'Cooking up a delicious, steaming pile of $(cblue)hlwm$(cnorm) for $(cblue)$(HOST)$(cnorm)'

	[ ! -d '$(HOST)/' ] && mkdir -v '$(HOST)/' || :
	m4 -D 'HOST'='$(HOST)' \
	   -D 'NAME'='hlwm' \
        < '$(BASE_FILE)' > '$(HOST)/autostart'

allhosts:
	if [ ! -f '$(HOSTS_FILE)' ]; then \
		echo 'No hosts file found ($(HOSTS_FILE))'; \
		exit 1; \
	else \
		while read -r host; do \
			make HOST="$$host"; \
		done < '$(HOSTS_FILE)'; \
	fi

addme:
	echo 'Adding $(cblue)$(HOST)$(cnorm) to $(HOSTS_FILE) ($(NAME))'
	echo '$(HOST)' >> '$(HOSTS_FILE)'

clean:
	cat '$(HOSTS_FILE)' | sort | uniq > '$(HOSTS_FILE).tmp'
	mv '$(HOSTS_FILE).tmp' '$(HOSTS_FILE)'

link: all
	if [ -z "$(FORCE)" ]; then \
		ln -sv '$(HOST)/autostart' '$(DEST)'; \
	else \
		ln -svf '$(HOST)/autostart' '$(DEST)'; \
	fi
